package Service;

import java.util.List;

import Dao.SelectionDao;
import Dao.SelectionDaoImpl;
import Entity.Experiment;
import Entity.Selection;

public class SelectionService {
	public static SelectionDaoImpl dao = null;
	public SelectionService(){dao = new SelectionDao();}
	public List<Selection> findAllSelection(){//返回所有选课表单
		
		return dao.findAllSelection();
	}
	public Selection findById(int id){// 根据id返回选课表
		
		return dao.findById(id);
	}
	public List<Selection> findByStu_number(String stu_number){//根据学生学号查找学生实验记录
		
		return dao.findByStu_number(stu_number);
	}     
    public List<Selection> findByEx_id(int ex_id){// 查找某个实验的记录
    	
		return dao.findByEx_id(ex_id);
    }
public Selection findByn_e(int ex_id,String stu_number){// 查找某个实验的记录
	List<Selection> list1=dao.findByEx_id(ex_id);
	List<Selection> list2=dao.findByStu_number(stu_number);
	for(int i=0;i<list1.size();i++){
		for(int j=0;j<list2.size();j++){
			if(list1.get(i).getId()==list2.get(j).getId()){
				return list1.get(i);
			}
		}
	}
	return null;
		//return dao.findByEx_id(ex_id);
    }
    
    public List<Selection> findByIsreport(int isreport){//查找是否提交报告的记录  0：未提交   1：已提交
    	
    	return dao.findByIsreport(isreport);
    }    
    public boolean addSelection(Selection selection){//添加记录
    	
    	return dao.addSelection(selection);
    }
    public boolean updateSelection(Selection selection){//更新记录
    	
    	return dao.updateSelection(selection);
    }
    public boolean deleteSelection(Selection selection){//删除记录
    	
    	return dao.deleteSelection(selection);
    }
    
    public boolean uploadFile(int sid,String fname){
    	Selection selection = dao.findById(sid);
    	selection.setIsreport(1);
    	selection.setStuURL(fname);
    	return dao.updateSelection(selection);
    }
 
    public boolean setGrade(int sid,int grade){
    	Selection selection = dao.findById(sid);
    	selection.setGrade(grade);
    	return dao.updateSelection(selection);
    	
    }
    

//    public boolean deleteSelection(Selection selection){//删除记录(根据学生id和实验id)
//    	SelectionDao dao=new SelectionDao();
//    	Experiment tmp=new Experiment();
//    	tmp.setBegweek(0);tmp.setEndweek(0);tmp.setId(selection.getEx_id());tmp.setName(null);tmp.set
//    	return dao.deleteSelection(selection);
//    }
    //新增，根据某一时间，某一实验id返回相应的数量
    int findChosedRoomNum(int week,int day,int section,String roomid){
    	
    	List<Selection> list=dao.findAllSelection();
    	int sum=0;
    	for(int i=0;i<list.size();i++){
    		Selection t=list.get(i);
    		if(t.getWeek()==week&&t.getDay()==day&&t.getSection()==section&&(t.getRoomid()).equals(roomid)){
    			sum++;
    		}
    	}
    	return sum;
    }
    
    public boolean addSelection(String uid,String uname,Experiment e,int week,int day,int section,String room){
        Selection kept=null;
        
    	Selection selection = new Selection(uid,uname,e.getId(),e.getName());
        selection.setWeek(week);
        selection.setDay(day);
        selection.setSection(section);
        selection.setRoomid(room);
        return dao.addSelection(selection);
    }

    public boolean delSeletion(String uid,int id){
        return dao.deleteOne(uid, id);
    }
    
	public static void main(String args[]){
		SelectionService ser=new SelectionService();
		List<Selection> lists=ser.findAllSelection();//.findByEx_id("");
		Selection s=ser.findById(29);
		
		
		s.setIsreport(1);
		ser.updateSelection(s);
//		ser.deleteSelection(s);
//		for(int i=0;i<lists.size();i++){
		//	Selection tmp=lists.get(i);
//			if(tmp.getStu_number().equals("618")) tmp.setStu_name("陈欣怡");
//			else if(tmp.getStu_number().equals("623")) tmp.setStu_name("郭天一");
//			else if(tmp.getStu_number().equals("655")) tmp.setStu_name("李宜贞");
//			if(tmp.getStu_number().equals("734")) tmp.setIsreport(0);
//			ser.updateSelection(tmp);
//			System.out.println("学生id:"+tmp.getStu_number()+"  实验id："+tmp.getEx_id()+"  老师id"+tmp.getEx_name()+"id"+tmp.getEx_id());
//		}
	//}
		}
	
	
}
